package beikai.com.assistant.common.util;

import beikai.com.assistant.common.exception.EleException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 配置返回對象
 */
public class R<T> {

    public static final String CODE = "code";
    public static final String DATA = "data";
    public static final String LIST = "list";
    public static final String MSG = "msg";
    public static final Integer SUCCESS = 10000;

    Map<String, Object> params;

    public R() {
        params = new HashMap<>();
    }

    public R(String key, Object value) {
        params = new HashMap<>();
        params.put(key, value);
    }

    public static JSONObject success() {
        return success(SUCCESS, "成功");
    }

    public static JSONObject success(Object data) {
        return success(SUCCESS, data);
    }

    public static JSONObject success(Integer code, Object data) {
        JSONObject result = new JSONObject();
        result.put(CODE, code);
        result.put(DATA, data);
        return result;
    }

    public static JSONObject successPage(Long pages,Long total, List data) {
        JSONObject result = new JSONObject();
        return success(SUCCESS, JsonPage.restPage(pages,total, data));
    }

    public static JSONObject fail(Integer code, String message) {
        JSONObject result = new JSONObject();
        result.put(CODE, code);
        result.put(MSG, message);
        return result;
    }


    public static JSONObject fail(Integer code, String message, Object data) {
        JSONObject result = new JSONObject();
        result.put(CODE, code);
        result.put(MSG, message);
        result.put(DATA, data);
        return result;
    }

    public R put(String key, Object data) {
        this.params.put(key, data);
        return this;
    }

    public static <T> T isOk(JSONObject jsonObject, Class<T> tClass) {
        if (jsonObject == null || null == jsonObject.getInteger(CODE)) {
            throw new EleException();
        }
        if (jsonObject.getInteger(CODE).equals(SUCCESS)) {
            return jsonObject.getObject(DATA,tClass);
        } else {
            throw new EleException(jsonObject.getString(MSG), jsonObject.getInteger(CODE));
        }
    }

    public static <T> List<T> isOkOfList(JSONObject jsonObject, Class<T> tClass) {
        if (jsonObject == null || null == jsonObject.getInteger(CODE)) {
            throw new EleException();
        }
        if (jsonObject.getInteger(CODE).equals(SUCCESS)) {
            return JSONArray.parseArray(jsonObject.getJSONArray(DATA).toString(), tClass);
        } else {
            throw new EleException(jsonObject.getString(MSG), jsonObject.getInteger(CODE));
        }
    }

    public static <T> PageInfo<T> isOkOfPageInfo(JSONObject jsonObject, Class<T> tClass) {
        if (jsonObject == null || null == jsonObject.getInteger(CODE)) {
            throw new EleException();
        }
        if (jsonObject.getInteger(CODE).equals(SUCCESS)) {
            return JSONObject.parseObject(jsonObject.getJSONObject(DATA).toString(),new TypeReference<PageInfo<T>>(tClass){});
        } else {
            throw new EleException(jsonObject.getString(MSG), jsonObject.getInteger(CODE));
        }
    }

    public static String isOk(JSONObject jsonObject) {
        if (jsonObject == null || null == jsonObject.getInteger(CODE)) {
            throw new EleException();
        }
        if (jsonObject.getInteger(CODE).equals(SUCCESS)) {
            return jsonObject.getJSONObject(DATA).toString();
        } else {
            throw new EleException(jsonObject.getString(MSG), jsonObject.getInteger(CODE));
        }
    }
}

class JsonPage<T> {
    private Long totalPage;
    private Long total;
    private List<T> list;

    public static <T> JsonPage<T> restPage(Long pages,Long total, List<T> list) {
        JsonPage<T> result = new JsonPage<T>();
        result.setTotalPage(pages);
        result.setTotal(total);
        result.setList(list);
        return result;
    }

    public Long getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Long totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }
}